$(document).ready(function(){
	showNum = 5;
	lang=$("#display-lang").val();
	userName = getUser();
    init();
    
    $("#searchq").click(function(){
    	$(".container").hide();
    	var loadDiv = $('<div class="load-data2"></div>');
    	$(".container").after(loadDiv);
    	$(this).parent().parent().submit();
    	
    });
    
   /*input hints*/
	$( "#query" ).autocomplete({
		source: function( request, response ) {
			 var data={};
			 data["kw"] = request.term;
			 data["lang"] = lang;
			 if( getUser()!=null){
				 data["userName"]  = getUser();
			 }
		     data =  JSON.stringify(data);
			 data=("hint="+encodeURIComponent(data));
			$.ajax({
				url: WEBAPP+"entity",
				dataType: "json",
				data: data,
				type:"post" ,
				success: function( data ) {
					response(data.result);
				}
			});
		},
		minLength:1
		})
	   .data( "autocomplete" )._renderItem = function( ul, item ) {
			var li = $("<li class='hitem lentity'></li>");
			var div = $('<div class="split"></div>');
			var a = $('<a></a>');
			var href =WEBAPP+"entity/";
			var genre = item.genre;
			var lang = item.lang;
			var id = item.id;
			if(genre==COLLECTION_NO)
				href=href+COLLECTION_STR;
			else if(genre==CUSTOM_ENTITY_NO)
				href = href+CUSTOM_ENTITY_STR;
			else if(genre==URI_ENTITY_NO)
				href= href+URI_ENTITY_STR ;
			href =href+id+"?lang="+lang;
			a.attr("href",href) ;			
			div.append(a);
			var spanName = $("<span class='entity'></span>") ;
			spanName.text(item.name);
			var spanType = $("<span class='type'></span>");
			var type = item.type;
			var typeLabel = "" ;
			for(j=0;j<type.length;j++){
				var mtype  =type[j] ;
				typeLabel=typeLabel+mtype;
				if(j!=type.length-1)
					typeLabel=typeLabel+" , " ;
			}
			spanType.text(typeLabel) ;
			a.append(spanName);
			a.append(spanType);
			li.append(div);
			li.appendTo(ul);
			ul.find("li:last>div").addClass("final");
			return li;
		};

	$(".hitem a").live("click",function(){
		window.location =$(this).attr("href");
	});
	
	
		
   /*navigation tabs*/
	$(".tab a").click(function(){
		 if(!$(this).parent().hasClass("selected")){
			   $(".tab-header .tab.selected").each(function(){
				   $(this).removeClass("selected");
			   });
			   $(this).parent().addClass("selected");
		   }
		 
		   if($(this).hasClass("pe")){
				 //process the popular entities
				  showEntities("popAll",0,showNum,lang,true);
			   }else if($(this).hasClass("me")){
				 //process mine entities
				  showEntities("mineAll",0,showNum,lang,true,userName);
		   }
	});
	
   $(".entity-list li,.hitem").live("mouseover",function(){
	   $(this).addClass("bh");
	   $(this).addClass("hover");
   });
   
   $(".entity-list li,.hitem").live("mouseleave",function(){
	   $(this).removeClass("bh");
	   $(this).removeClass("hover");
   });
   
   $(".moreDiv").live("mouseover",function(){
	   $(this).addClass("hover");
   });
   
   $(".moreDiv").live("mouseleave",function(){
	   $(this).removeClass("hover");
   });
   
   $(".entity-list li").live("click",function(){
	   window.location  = $(this).find(".link").attr("loc");
   });
   
});

function init(){
	if($(".me").length>0){
		$(".me").parent().addClass("selected");
		showEntities("mineAll",0,showNum,lang,true,userName);
		
    }else  {
	   $(".pe").parent().addClass("selected");
	   showEntities("popAll",0,showNum,lang,true);
    }
}


function showEntities(optype, start, length,lang,newFlag,user){
	 var data={};
	 data["type"] = optype;
	 data["start"] = start;
	 data["length"] = length;
	 data["lang"] = lang;
	 data["user"] = user;
     data =  JSON.stringify(data);
	 data=("entity="+encodeURIComponent(data));
	 $.ajax({
	  url:WEBAPP+"entity",
	  async:true,
	  data:data,
	  dataType:"json",
	  type:"post" ,
	  beforeSend:function(){
		  var element ;
		  if($(".entity-list").parent().find(".more-container").length>0)
			  element = $(".more-container");
		  else
			  element = $(".entity-list");
		  if(newFlag)
			  element =  $(".entity-list");
		  element.html("");
		  element.addClass("loading2");
	  },
	  success:function(result){
		  var ele ;
		  if($(".entity-list").parent().find(".more-container").length>0)
			  ele = $(".more-container");
		  else
			  ele = $(".entity-list");
		  if(newFlag)
			  ele =  $(".entity-list");
		  ele.removeClass("loading2");
		  var array = result["result"];
		  var hasMore = result["hasMore"];
		  var flag = false;
		  if(newFlag){
			  $(".entity-list").html("");
		  }
		  if($(".entity-list ol").length>0)
			  flag =true;
		  if(!flag){
			  var olt = $("<ol></ol>");
			  $(".entity-list").append(olt);
		  }
		  var ol  = $(".entity-list>ol") ;
		  for(i=0;i<array.length;i++){
			   var element = array[i];
			   var genre = element["genre"];
			   var id = element["id"];
			  
			   var a = $("<span class='link'></span>");
			   var href =WEBAPP+"entity/";
			   var eid ;
			   if(genre==COLLECTION_NO){
				   href=href+COLLECTION_STR;
				   eid=COLLECTION_STR ;
			   }
			   else if(genre==CUSTOM_ENTITY_NO){
				   href = href+CUSTOM_ENTITY_STR;
				   eid=CUSTOM_ENTITY_STR ;
			   }
			   else if(genre==URI_ENTITY_NO){
				   href= href+URI_ENTITY_STR ;
				   eid=URI_ENTITY_STR ;
			   }
			   href =href+id;
			   if(lang=="zh")
				   href = href+"?lang=zh" ;
			   eid= eid+id;
			   a.attr("loc",href) ;
			   var name  = element["name"];
			   if(ol.find("li[eid="+eid+"]").length>0){
				   if(ol.find("li[eid="+eid+"]").find(".entity").html().trim()==name){
					   continue;
				   }
			   }
			   
			   var li = $("<li></li>");
			   li.attr("eid",eid);
			   ol.append(li);
			 
			   if(genre==COLLECTION_NO){
				   li.addClass("lcollection");
			   }else
				   li.addClass("lentity");
			   
			   var div = $('<div class="sc split"></div>');
			   li.append(div) ;
			   
			   div.append(a) ;
			   var spanName = $("<span></span>") ;
			   var spanType = $("<span class='type'></span>");
			   if(name==""){
				   spanName.addClass("vacant");
				   if(lang=="en"){
					   name = "Entity"+id;
				   }else if(lang=="zh"){
					   name = "实体"+id;
				   }
			   }
			   spanName.text(name);
			   if(genre==COLLECTION_NO){
				   spanName.addClass("collection");
			   }else{
				   spanName.addClass("entity");
				   if(genre==CUSTOM_ENTITY_NO){
					   spanName.addClass("custom");
				   }else{
					   spanName.addClass("uri");
				   }
			   }
			   a.append(spanName);
			   var type = element["type"] ;
			   for(j=0;j<type.length;j++){
				   var span = $("<span></span>");
				   spanType.append(span);
				   var mtype  =type[j] ;
				   span.text(mtype["label"]);
				   span.attr("type_id",mtype["type_id"]);
				   if(j!=type.length-1){
					   var span = $("<span>, </span>");
					   spanType.append(span);
				   }
			   }
			   div.append(spanType);
		  }
		  $(".entity-list ol li:last>div").addClass("final");
		  $(".more-container").remove();
		  if(hasMore==true){
			  var div = $("<div class='more-container'></div>");
			  var moreDiv = $("<div class='moreDiv'></div>");
			  var a = null;
			  if(lang=='en')
				  a =  $('<span class="box"><span class="tip">Load More</span><span class="arrowDown"></span></span>');
			  else if(lang=='zh')
				  a =  $('<span class="box"><span class="tip">更多</span><span class="arrowDown"></span></span>'); 
			  moreDiv.append(a);
			  div.append(moreDiv);
			  a.parent().bind("click",function(){
				  $(".entity-list ol li>div.final").removeClass("final");
				  showEntities(optype, start+length, length,lang,false,user);
			  });
			  $(".entity-list").parent().append(div);
		  }
	  }
	});
}





